package com.atguigu.registration.user.api;

import com.atguigu.registration.common.result.Result;
import com.atguigu.registration.common.utils.AuthContextHolder;
import com.atguigu.registration.model.user.UserInfo;
import com.atguigu.registration.user.service.UserInfoService;
import com.atguigu.registration.vo.user.LoginVo;
import com.atguigu.registration.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: Jiang Yizhi
 * @Date: 2022/5/3 4:37 PM
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /** 用户手机号登录接口 */
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info  = userInfoService.loginUser(loginVo);
        return Result.ok(info);
    }

    /** 用户认证接口 */
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        /** 传递两个参数，第一个用户ID，第二个参数认证数据vo对象 */
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /** 获取用户id信息 */
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

}
